package com.mantru.iotpot.Begin;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.provider.Settings;
import android.util.Log;

import androidx.appcompat.app.AppCompatActivity;

import com.mantru.iotpot.ApplicationClass;
import com.mantru.iotpot.R;
import com.mantru.iotpot.Util.MotherboardUtil;
import com.mantru.iotpot.Util.SharePreUtil;
import com.mantru.iotpot.setting.WiFiActivity;

public class FirstActivity extends AppCompatActivity {

    private Handler mHandler = new Handler();
    private ApplicationClass ac;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        //检查是否已经授予权限，大于6.0的系统适用，小于6.0系统默认打开，无需理会
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
            //没有权限，须要申请权限，由于是打开一个受权页面，因此拿不到返回状态的，因此建议是在onResume方法中重新执行一次校验
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
            intent.setData(Uri.parse("package:" + getPackageName()));
            startActivity(intent);
        }
        ac = (ApplicationClass) getApplication();// 初始化 ApplicationClass
        ac.APP_Init();
        //读取状态栏展示状态
//        String barState = SharePreUtil.INSTANCE.getStringData(ac, "barState");
//        MotherboardUtil.INSTANCE.showBarState(ac, barState != null && !barState.isEmpty() && !barState.equals("false"));
        MotherboardUtil.INSTANCE.showBarState(ac, false);
        int DeviceStatus = ac.DeviceStatus;//获取界面状态
        Log.d("DeviceStatus","DeviceStatus--> " + DeviceStatus);
        // 使用Handler的postDelayed方法，3秒后执行runnable
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (ac.DeviceStatus == -1){
                    int net = ac.net_work_status;//获取当前网络连接的状态
                    if (net == 1){//网络正常
                        Intent intent = new Intent(FirstActivity.this, LoginActivity.class);
                        startActivity(intent);
                        // 关闭当前Activity
                        finish();
                    } else if (net == 0) {
                        Intent intent = new Intent(FirstActivity.this, WiFiActivity.class);
                        intent.putExtra("net",1);
                        startActivity(intent);//跳转到设置wifi界面
                    }
                }else {
                    Intent intent = new Intent(FirstActivity.this, BeginSetActivity.class);
                    startActivity(intent);
                    // 关闭当前Activity
                    finish();
                }

            }
        }, 3000); // 3000毫秒 = 3秒
    }
}